﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace g2
{
    public partial class Form1 : Form
    {
        private static string id;
        public Form1()
        {
            InitializeComponent();
        }

        public static bool isRN(int year)
        {
            if ((year % 4 == 0 && year % 100 != 0)|year%400==0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool idIsLegal(string str_id)
        {
            String pattern1 = @"([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9X]$";
            if (Regex.IsMatch(str_id, pattern1))
            {
                String year_s = Convert.ToString(str_id[6]) + Convert.ToString(str_id[7]) + Convert.ToString(str_id[8]) + Convert.ToString(str_id[9]);
                int y = Convert.ToInt32(year_s);
                String month_s;
                String day_s;
                if (str_id[10] == 0)
                {
                    month_s = Convert.ToString(str_id[11]);
                }
                else
                {
                    month_s = Convert.ToString(str_id[10]) + Convert.ToString(str_id[11]);
                }
                if (str_id[12] == 0)
                {
                    day_s = Convert.ToString(str_id[13]);
                }
                else
                {
                    day_s = Convert.ToString(str_id[12]) + Convert.ToString(str_id[13]);
                }

                int m = Convert.ToInt32(month_s);
                int d = Convert.ToInt32(day_s);
                if (!isRN(y) & m == 2 & d > 28)
                {
                    //Console.WriteLine("非闰年，2月有28天");
                    DialogResult result1 = MessageBox.Show("日期错误，非闰年，2月有28天", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
                if (m == 2 & d > 29)
                {
                    //Console.WriteLine("闰年2月只有29天");
                    DialogResult result2 = MessageBox.Show("日期错误，闰年2月只有29天", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
                if ((m == 4 || m == 6 || m == 9 || m == 11) & d > 30)
                {
                    //Console.WriteLine("小月只有30天");
                    DialogResult result3 = MessageBox.Show("日期错误，小月只有30天", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
                DialogResult result = MessageBox.Show("匹配成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;
            }
            else
            {
                //Console.WriteLine("不匹配");
                DialogResult result4 = MessageBox.Show("身份证号不合法，匹配失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }

        private void text_id_TextChanged(object sender, EventArgs e)
        {
            id = text_id.Text;
        }

        private void btn_submit_Click(object sender, EventArgs e)
        {
            idIsLegal(id);
        }
    }
}
